function closeShareModal(){
	if($("div#share-modal").length>0){
		$("div#share-modal").remove();
		return false;
	}
}
$(document).keydown(function(e){
	if(e.keyCode==27){
		closeShareModal();
	}
}); 
function showShare(target){
	if($("div#share-modal").length>0){
		$("div#share-modal").remove();
		return false;
	}
	
	var container = $('<div id="share-modal"></div>')
		.append('<div class="modal-backdrop"></div>');
	var modal = $('<div class="modal" style="width: 600px; padding: 40px;"></div>');
	var left = ($(window).width()-680)/2;  
	var top = $(window).scrollTop() + 100;   
	modal.css({"left": left+"px","top": top+"px"});
	
	modal.append($("#shareText").clone().show());
	container.append(modal);
	container.appendTo(document.body); 
}

function showCircleMenu(event, target){
	$(target).addClass("hidden");
	var shareCircle = $(target).parent().parent();
	shareCircle.children("div.circle-menu").removeClass("hidden");
	$(".circle-editor input",shareCircle).removeClass("hidden").focus();
	event.stopPropagation();
}

function removeCircle(event,target){
	var $menu = $(target).parent().parent();
	recycleMenu($menu);
	$menu.remove();
	event.stopPropagation();
}

function tryRemoveCircle(event,target){  
	if(event.keyCode==8){ 
		var text = $(target).val();
		if(text == ""){
			var $menu = $(target).parent().children("ul").children("li:last-child");
			recycleMenu($menu);
			$menu.remove();
		}
	}
}
function recycleMenu($menu){ 
	var shareCircle = $menu.parent().parent().parent();
	$(".circle-menu ul li[value="+$menu.val()+"]",shareCircle).removeClass("hidden");
}
function circleMenuClicked(event,target){
	var cur = $(target); 
	var text = cur.text();
	var shareCircle = cur.parent().parent().parent();
	if(cur.hasClass("menu-plus")){
		var item = '<li value="'+cur.val()+'"><span class="stream-open"><span class="plus">+</span>'+text+'<span class="close" onclick="removeCircle(event,this)">×</span></span></li>';
	}
	if(cur.hasClass("menu-circle")){
		var item = '<li value="'+cur.val()+'"><span class="stream-circle"><span class="circle">O</span>'+text+'<span class="close" onclick="removeCircle(event,this)">×</span></span></li>';
	}
	if(cur.hasClass("menu-district")){
		var item = '<li value="'+cur.val()+'"><span class="stream-district"><span class="to">»</span>'+text+'<span class="close" onclick="removeCircle(event,this)">×</span></span></li>';
	} 
	$(".circle-editor ul",shareCircle).append(item);
	
	if(cur.next().hasClass("divider") && cur.prev().hasClass("divider")){
		cur.next().addClass("hidden");
	}
	cur.addClass("hidden");
	$(".circle-editor input",shareCircle).removeClass("hidden").focus();
	event.stopPropagation();
}
 

$(function(){   
	$('body').click(function(event){
		if($(".share-circle .circle-menu").is(":visible")){
			$(".circle-editor .hint").removeClass("hidden");
			$(".share-circle .circle-menu").addClass("hidden");
			$(".share-circle .circle-input").addClass("hidden");
		}
	}); 

	$(".tweet-action .command").click(function (e) {
		showReply($(this)); 
		return false;
	}); 
	$(".reply-box .cancel").click(function (e) {
		hideReply($(this).parent().parent());
		return false;
	}); 
	
	$(".tweet-action .action-links .reply-link").click(function (e) {
		showReply($(this).parent().parent().children("div.command")); 
		return false;
	}); 
	
	$(".reply-item .command").click(function (e) {
		
		showReply($(this).parent().parent().parent().children("div.command")); 
		return false;
	}); 
	
	$(".stock-list ul li").mouseover(function(event){
		$(this).children(".close").removeClass("hidden");
	});
	$(".stock-list ul li").mouseleave(function(event){
		$(this).children(".close").addClass("hidden");
		
	});
	  
});

function showReply($cmdbox){
	$cmdbox.children("input").hide();
	$cmdbox.children(".reply-box").show();
}
function hideReply($cmdbox){
	$cmdbox.children("input").show();
	$cmdbox.children(".reply-box").hide();
} 